package ro.imc.rest.client.display.components;

import ro.imc.rest.client.display.util.DialogForm;
import ro.imc.rest.client.display.util.MessageConstants;
import ro.imc.rest.client.services.forserver.RestService;
import ro.imc.rest.client.services.forserver.RestServiceAsync;
import ro.imc.rest.shared.dto.LearningObject;
import ro.imc.rest.shared.dto.LearningObjectStatus;
import ro.imc.rest.shared.dto.RestLogin;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Widget;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.TabPanel;


public class CourseInfoForm extends Composite {

	private static CourseInfoFormUiBinder uiBinder =
		GWT.create(CourseInfoFormUiBinder.class);
	@UiField Label strCourseName;
	@UiField AbsolutePanel welcomePanel;
	@UiField Label strObjectId;
	@UiField Label strMetatags;
	@UiField Label strDescription;
	@UiField Label strLocation;
	@UiField Label strEndDate;
	@UiField Button btnStartCourse;
	@UiField Button btnFinishCourse;
	@UiField Label strStatus;
	@UiField Label strStartDate;
	@UiField TabPanel tabbedPanel;
	
	private RestLogin user;
	private LearningObject obj;
	private RestServiceAsync service = GWT.create(RestService.class);
	interface CourseInfoFormUiBinder extends UiBinder<Widget, CourseInfoForm> {
	}

	public CourseInfoForm(RestLogin user, LearningObject obj) {
		initWidget(uiBinder.createAndBindUi(this));
		this.user = user;
		this.obj = obj;
		tabbedPanel.selectTab(0);
		populateFrom();
		
	}
	
	private void populateFrom() {
	  strCourseName.setText(obj.getName());
	  strStartDate.setText(obj.getStartDate());
	  strObjectId.setText(obj.getId());
	  strMetatags.setText(obj.getMetatags().toString());
	  strDescription.setText(obj.getDescription());
	  strLocation.setText(obj.getLocation());
	  strEndDate.setText(obj.getEndDate());
	  strStatus.setText(obj.getStatus().toString());
	  btnStartCourse.setEnabled(false);
	  btnFinishCourse.setEnabled(false);
	  switch (obj.getStatus()) {
	  case BOOKED: {
		  btnStartCourse.setEnabled(true);
		  btnStartCourse.addClickHandler(new StartCourseClickHandler());
		  btnFinishCourse.setEnabled(false);
	  }
	  case FINISHED: {
		  btnStartCourse.setEnabled(false);
		  btnFinishCourse.setEnabled(false);
	  }
	  case STARTED: {
		  btnStartCourse.setEnabled(false);
		  btnFinishCourse.setEnabled(true);
		  btnFinishCourse.addClickHandler(new FinishCourseClickHandler());
	  }
	  }
	}
	
	private class StartCourseClickHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {
			service.startCourse(user, obj.getId(), new AsyncCallback<Void>() {
				
				@Override
				public void onSuccess(Void result) {
					btnStartCourse.setEnabled(false);
					btnFinishCourse.setEnabled(true);
					btnFinishCourse.addHandler(new FinishCourseClickHandler(), ClickEvent.getType());
					strStatus.setText(LearningObjectStatus.STARTED.toString());
					
				}
				
				@Override
				public void onFailure(Throwable caught) {
				DialogForm.displayDialog(MessageConstants.REST_ERR_TITLE, 
						caught.getMessage(),
						MessageConstants.REST_ERR_MESSAGE, 
						MessageConstants.REST_ERR_TIP);
					
				}
			});
			
		}
		
	}
	private class FinishCourseClickHandler implements ClickHandler {

		@Override
		public void onClick(ClickEvent event) {

		service.finishCourse(user, obj.getId(), new AsyncCallback<Void>() {
				
				@Override
				public void onSuccess(Void result) {
					btnFinishCourse.setEnabled(false);
					strStatus.setText(LearningObjectStatus.FINISHED.toString());
					
				}
				
				@Override
				public void onFailure(Throwable caught) {
				DialogForm.displayDialog(MessageConstants.REST_ERR_TITLE, 
						caught.getMessage(),
						MessageConstants.REST_ERR_MESSAGE, 
						MessageConstants.REST_ERR_TIP);
					
				}
			});
			
		}
		
	}
}
